package com.blinddate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blinddate.context.BaseContext;
import com.blinddate.mapper.InterestMapper;
import com.blinddate.mapper.RelevancyMapper;
import com.blinddate.mapper.UserInfMapper;
import com.blinddate.mapper.UserMapper;
import com.blinddate.pojo.entity.Interest;
import com.blinddate.pojo.entity.Relevancy;
import com.blinddate.pojo.entity.User;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.vo.UserInfVO;
import com.blinddate.service.UserInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfServiceImpl extends ServiceImpl<UserInfMapper,UserInf> implements UserInfService {

    @Autowired
    private UserInfMapper userInfMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RelevancyMapper relevancyMapper;
    
    @Autowired
    private InterestMapper interestMapper;

    private final static Integer PAGE_SIZE=10;

    public List<UserInfVO> getUserList(Long userId, Integer gender){
        List<UserInf> userInfList = userInfMapper.getList(gender);
        List<UserInfVO> userInfVOS = new ArrayList<>();
        for (UserInf userInf:userInfList) {
            UserInfVO userInfVO1 = new UserInfVO();
            userInfVO1.setUserInf(userInf);
            UserInfVO userInfVO = showAllInf(userInfVO1, userId, userInf.getUserId());
            userInfVOS.add(userInfVO);
        }
        return userInfVOS;
    }

    public UserInfVO getOneUser(Long userId,Long getId){
//        获取指定用户数据
        UserInf userInf = getUserInf(getId);
        UserInfVO userInfVO =new UserInfVO();
        userInfVO.setUserInf(userInf);
//       判断是否全部展示用户的数据
        userInfVO = showAllInf(userInfVO,userId,getId);
        return userInfVO;
    }

//     获取用户数据的函数  使用到缓存
    @Cacheable(cacheNames = "useInfCahe",key = "#userId")
    public UserInf getUserInf(Long userId){
        LambdaQueryWrapper<UserInf> userInfLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfLambdaQueryWrapper.eq(UserInf::getUserId,userId);
        UserInf userInf = userInfMapper.selectOne(userInfLambdaQueryWrapper);
        return userInf;
    }

    //    是否展示用户所有信息
    public UserInfVO showAllInf(UserInfVO userInfVO, Long userId, Long getId){
        LambdaQueryWrapper<Relevancy> relevancyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        relevancyLambdaQueryWrapper.eq(Relevancy::getUserId,userId).eq(Relevancy::getGetId,getId);
        Relevancy relevancy = relevancyMapper.selectOne(relevancyLambdaQueryWrapper);
        LambdaQueryWrapper<Interest> interestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        interestLambdaQueryWrapper.eq(Interest::getUserId,userId).eq(Interest::getGetId,getId);
        Interest interest = interestMapper.selectOne(interestLambdaQueryWrapper);
        if (relevancy != null){
            userInfVO.setShowAll(1);
        }
        if (interest != null){
            userInfVO.setInterest(1);
        }
        return userInfVO;
    }

    public Integer uploaduserInf(UserInf userInf){
        int insert = userInfMapper.insert(userInf);
        return insert;
    }

    /**
     * 解锁用户信息
     *
     * @param id
     * @return
     */
    public UserInfVO unlock(Long id) {
        User user=userMapper.selectById(1686016376910659585L);
        Integer count =user.getCount();
        UserInfVO userInfVO=new UserInfVO();
        if(count<1){
            return null;
        }else{
            Relevancy relevancy=new Relevancy();
            relevancy.setUserId(1686016376910659585L);
            relevancy.setGetId(id);
            relevancyMapper.insert(relevancy);
            count--;
            user.setCount(count);
            userMapper.updateById(user);
        }
        LambdaQueryWrapper<UserInf> lqw=new LambdaQueryWrapper<UserInf>();
        lqw.eq(UserInf::getUserId,id);
        UserInf userInf=userInfMapper.selectOne(lqw);
        userInfVO.setUserInf(userInf);
        return userInfVO;
    }
}
